package de.nabble.gui;

import java.awt.Color;
import java.util.HashMap;
import java.util.Map;

public class ColorTheme {
	
	private Map<String, Color> colors;
	private Color base;
	private int baseInt = 0x00ff00;
	
	private void calculateColors() {
		if(base != null) {
			Color normalField = base;
			Color doubleWord = getInvert(base);
			Color tripleWord = new Color(doubleWord.getRed(), 
					doubleWord.getGreen(), 0);
			Color doubleLetter = getInvert(tripleWord);
			Color tripleLetter = new Color(doubleLetter.getRed(), 
					0, doubleLetter.getBlue());
			
			colors.put("normalField", normalField);
			colors.put("doubleWord", doubleWord);
			colors.put("tripleWord", tripleWord);
			colors.put("doubleLetter", doubleLetter);
			colors.put("tripleLetter", tripleLetter);
			colors.put("star", Color.yellow);
			colors.put("boardBgColor", Color.ORANGE);
			colors.put("stoneBgColor", Color.WHITE);
			colors.put("selectedStoneBgColor", 
					colors.get("stoneBgColor").darker());
			colors.put("stoneFgColor", Color.BLACK);
		}
	}
	
	private Color getInvert(Color base) {
		return new Color(Color.WHITE.getRed() - base.getRed(),
		 Color.WHITE.getGreen() - base.getGreen(),
		 Color.WHITE.getBlue() - base.getBlue());
	}
	
	public Color getColor(String colorKey) {
		if(colors.containsKey(colorKey))
			return colors.get(colorKey);
		
		throw new NoSuchColorException("A color with key " + colorKey 
										+ " doesn't exist!");
	}
	
	/**singleton**/
	private ColorTheme() {
		colors = new HashMap<String, Color>();
		base = new Color(baseInt);
		calculateColors();
	}
	
	private static ColorTheme instance;
	public static ColorTheme getInstance() {
		if(instance == null) {
			instance = new ColorTheme();
		}
		
		return instance;
	}
	
	public static class NoSuchColorException extends RuntimeException {
		public NoSuchColorException(String msg) {
			super(msg);
		}
		private static final long serialVersionUID = 1L;
	}
}
